很多所谓的“液态玻璃”,其实只是给卡片加一层半透明背景和高斯模糊。

结果是背景颜色被糊掉了,边缘却仍然像一张平板贴纸;文字忽明忽暗,深浅模式各自像另一套组件。真正的玻璃感不来自“模糊多少像素”,而来自光穿过材料时,边缘、体积、颜色和内容共同形成的关系。

这套材质可以拆成六个可见层,再加一层对背景的自适应。

玻璃只适合有内容的背景

玻璃需要从背后取色、折射和变化。放在纯色背景上,它只剩下灰色遮罩,既没有厚度,也没有透明感。

因此只处理真正位于图片、壁纸、视频或复杂内容之上的表面:卡片、播放器、搜索栏、工具栏、菜单、Popover 和 Toast。纯色背景上的卡片继续使用实色 surface,这不是能力不足,而是材质的适用边界。

也要先把效果做进颜色 token,而不是为每个组件复制一套玻璃 CSS。表面通过使用同一组变量选择进入材质,深浅模式、悬停和强调状态才有共同语言。

六个可见层

完整的玻璃材质可以按光进入和离开的顺序理解:

  1. 投影:让容器有一点离开背景的体积,但不能变成普通卡片阴影。
  2. 模糊与提亮:模糊背后内容,同时提高饱和度与亮度,让颜色透进来,而不是糊成牛奶。
  3. 边缘折射:只在靠近边缘的一圈把背景向内压,越靠边越明显,中间保持平整。
  4. 着色:在玻璃内部叠一层半透明颜色,浓度根据背景复杂程度变化。
  5. 边缘光:左上更亮,右下稍暗,再往内收一层柔光,建立“厚板”的截面。
  6. 文字着色:玻璃上的文字、图标和进度也使用同一套 ink color 与混合模式,让它们像被压在材料里,而不是浮在表面。

原推文还列出了第七点:读取背景亮度,自动决定用深色还是浅色文字。它属于材质对环境的响应,不是额外的装饰层。

把材质做成状态 token

浅色玻璃、浅色模式覆盖深色区域、深色玻璃,需要三套状态。默认的高光玻璃可以使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
.glass-light {
--glass-tint: rgb(255 255 255 / 0.14);
--glass-filter: blur(2px) saturate(1.8) brightness(1.06);
--glass-ink: rgb(0 0 0 / 0.86);
--glass-ink-muted: rgb(60 60 67 / 0.7);
}

.glass-dark {
--glass-tint: rgb(20 20 24 / 0.28);
--glass-filter: blur(2px) saturate(1.6) brightness(0.8);
--glass-ink: rgb(255 255 255 / 0.94);
--glass-ink-muted: rgb(235 235 245 / 0.64);
}

这里只是起始值。实际密度应落在浅色玻璃白色 6–22%、深色玻璃 rgb(20 20 24) 16–38% 之间,并根据后面的背景调整。

每个表面只需要改变四项:

1
2
3
4
5
6
.glass {
background: var(--glass-tint);
backdrop-filter: var(--glass-filter);
box-shadow: var(--glass-edge);
color: var(--glass-ink);
}

如果边缘折射可用,把 url(#glass-lens) 加到 backdrop-filter 的最后。数字和颜色 token 可以用 @property 注册,让悬停、主题切换和密度变化平滑过渡,而不是跳值。

悬停时提高约 14% 的着色亮度,而不是抬起阴影。玻璃变亮,才像光源变化;阴影突然加深,只会重新变回普通卡片。

边缘折射决定它有没有厚度

backdrop-filter: blur() 只能均匀地处理整个矩形。现实中的玻璃边缘会把后方画面折向内侧,这才是“液态玻璃”最关键、也最难伪造的一层。

实现时,需要为每个表面生成一张位移贴图:

  • 红色通道表示横向弯曲;
  • 绿色通道表示纵向弯曲;
  • 128 表示不产生偏移;
  • 中间区域保持平坦,只在边缘形成透镜。

设边缘宽度为 rim,距离边缘 d 的点向内偏移:

1
1.1 × rim × (1 - d / rim)^2.6

rim 可以取约 44px,并按短边约束:

1
clamp(4px, min(44px, shortSide / 3.5), 44px)

偏移在真实边缘处最强,向内侧迅速衰减。贴图必须沿着与 CSS 完全相同的圆角生成;如果表面使用 corner-shape: superellipse(),折射路径也要遵循超级椭圆,否则转角处会出现边缘光和背景弯曲对不上的缝。

把贴图交给 SVG 的 feImage 与 feDisplacementMap,再放进 backdrop-filter。为了形成轻微色散,可以分三次处理:红光偏移多 15%,蓝光偏移少 15%,各自经过 feColorMatrix 留在自己的通道,最后用 screen 混合叠回。

色散是提醒“这是有厚度的材料”的细节,不是彩虹滤镜。背景越复杂、边缘越宽,越容易在转角处形成雾化,此时应当降低分色,而不是继续增加偏移。

目前只有 Chromium 对 backdrop-filter 中的 SVG url() 有可靠绘制能力。基础模糊必须作为所有浏览器都能使用的一层;只有在镜头能力确认可用时,再逐步叠加。

CSS.supports() 能排除完全无法解析属性的浏览器,但不能单独证明滤镜一定正确绘制。因此还需要在目标浏览器上实际检查边缘。不要因为折射不可用就放弃整块玻璃,回退到纯色着色、边缘光与阴影即可。

背景采样不是“取一个平均色”

玻璃背后的画面会变化,文字颜色和着色浓度也应该变化。

在每个表面对应的背景区域取 5 × 3 网格,先丢掉两个最暗和两个最亮的样本,再计算平均值,可以减少极端高光和阴影对判断的干扰。

每次采样可以得到三种信息:

  • 亮度:判断该用深色文字还是浅色文字;
  • 纹理:最亮点与最暗点的差值,决定玻璃需要多厚;
  • 色度:背景颜色偏离灰度的程度,决定着色是否应该更中性。

亮度可以用常用加权:

1
L = (0.299R + 0.587G + 0.114B) / 255

浅色模式下,背景亮度低于 0.5 时改用浅色文字。为了避免画面在临界点闪烁,需要给判断增加约 ±0.05 的滞后区;同一行相邻的表面共享一个决定,不能让一排卡片各自来回变色。

密度则可以把三个因素合在一起:

1
2
3
4
5
6
7
density = clamp(
0.55 × 背景靠近文字亮度的程度
+ 0.70 × 纹理
+ 0.30 × 色度,
0,
1
)

密度越高,着色越厚。背景越花,玻璃越需要承担可读性;背景越纯净,材质越可以薄。

同时降低背景对玻璃的色彩污染:

1
keptSaturation = max(0.45, 1 - 0.75 × chroma)

这样紫色晚霞或高饱和视频不会把玻璃染成一块彩色塑料。

采样应该跟随背景、主题和表面位置变化,但要节流到动画帧,不要在滚动或播放视频时无限重算。对于跨域图片、实时视频等无法读取像素的背景,回退到主题提供的 ink 与较高密度。

文字也是玻璃的一部分

玻璃上的文字、图标、时间和进度不是普通前景色。

浅色玻璃使用黑色 86% 作为主 ink、rgb(60 60 67) 70% 作为次要 ink,并配合 mix-blend-mode: multiply;深色玻璃使用白色 94% 和 rgb(235 235 245) 64%,配合 plus-lighter。

图标应该用不透明颜色绘制,再通过整体 opacity 淡化。直接给描边和填充同时设置半透明颜色,会让二者重叠的位置变暗,看起来像图形本身出现了污点。

浅色玻璃上的高亮行也不要用灰色。悬停时叠加一层更亮的白色玻璃,选中时进一步提高亮度并加上白色描边。状态变化反映的是玻璃厚度和受光变化,而不是在表面铺一层灰色遮罩。

可读性优先于透明

玻璃越透明,背景对文字的影响越大。

必须在产品允许的最繁忙、最暗背景上检查默认文本对比度。对比度不足时,优先增加着色密度,而不是继续提高文字不透明度。文字变实会破坏玻璃的分层,玻璃变厚仍然是同一个材质。

当系统开启减少透明度或强制颜色模式时,回退为同一色调的完全不透明表面。焦点环、禁用状态和错误色保持原样,失败状态不能被玻璃染色。

性能边界

边缘折射依赖 SVG 滤镜和多层背景处理,不应该无差别铺到所有元素。

位移贴图按圆角和边缘宽度生成一次后复用;窗口尺寸改变时移动贴图,不重新生成。先把贴图图像解码完成,再把表面指向滤镜,否则会先绘制成没有模糊的普通背景。

每个滤镜使用唯一 ID 和自己的 <svg>。小于 32px 的表面、本身带有 will-change 或独立合成层的菜单和动画面板,应跳过边缘镜头,只保留基础玻璃。这里让浏览器处理合成层,比强行加一个滤镜更稳定。

检查清单

  • 玻璃是否只覆盖在图片、视频或复杂背景上;
  • 材质是否进入了深色、浅色和覆盖深区的 token;
  • 投影、模糊、着色、边缘光、文字和折射是否各自职责清楚;
  • 边缘折射是否与 CSS 圆角或连续曲率一致;
  • 是否保留所有浏览器都能使用的基础模糊与实色回退;
  • 背景采样是否使用去极值平均、滞后阈值与节流;
  • 着色密度和饱和度是否能随背景复杂度变化;
  • 图标是否用不透明颜色整体淡出;
  • 最差背景下文字对比度是否通过,减少透明度与强制颜色是否正确回退;
  • 位移贴图是否缓存、正确解码,并跳过不适合合成的表面。

液态玻璃示例 Prompt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Prompt

Give the cards and panels in my product a liquid glass material: clear glass that takes its color from the scene behind it, reads as a thick slab at the rim, adapts to what is behind it, and keeps its text readable in light and dark mode. Work inside my existing design system. This is a change of surface, not of layout: keep my markup, spacing, radii, fonts, icons and copy.

0. Start from what exists
- Find every surface that sits over an image, a wallpaper, video or a busy background: cards, players, search bars, toolbars, menus, popovers, toasts. List them with their component and where they are.
- Find how the product switches between light and dark mode, and where its color tokens live. The glass goes into those tokens, not into each component.
- Leave surfaces over a plain background alone; glass over a flat color only looks gray.

1. The material as tokens
Build it as one set of variables per state, so a surface opts in by using them. The numbers are a starting point, for a UI at its normal size.
- Light glass, dark text (the default): tint white at 6–22% (by density, step 4); backdrop filter blur(2px) saturate(180%) brightness(1.06), so the color comes from behind and stays vivid instead of milky.
Edge: inset 1.5px 1.5px 1px -1px white 95%, inset -1.5px -1.5px 1px -1px white 55%, inset 0 0 0 0.5px white 30%, 0 0 0 0.5px black 6%, 0 6px 24px black 8%.
Inner light, on a layer inside the surface: a radial highlight from the top left (white 30% fading out by 55%), and soft inset glows just inside the rim, brighter at the top and left, a little shade at the bottom.
- Light mode over a dark area, light text: tint rgb(16 18 22) at 10–30%; filter blur(2px) saturate(170%), then brightness(0.85); edge: a faint inset white line on top and a 0.5px dark outline.
- Dark mode: tint rgb(20 20 24) at 16–38%; filter blur(2px) saturate(160%) brightness(0.8), which dims like a multiply with black instead of going muddy; the same edge and inner light at about a third of their strength, with a darker outline and shadow.
- Ink, one color in fixed steps, used for everything drawn on the glass:
on light glass black 86% (titles, icons, times, progress), rgb(60 60 67) at 70% (secondary); mix-blend-mode multiply;
on dark glass white 94%, rgb(235 235 245) at 64%; mix-blend-mode plus-lighter.
Draw icons in the opaque color and fade the whole icon with opacity: a translucent color darkens where a shape's strokes and fills overlap.
- A highlighted row on light glass is a lighter layer of the glass: white 42% on hover, 76% when selected, with a white stroke. Never a gray wash.
- Register the numeric and color tokens with @property so a surface glides between states instead of jumping.

2. Apply it in four declarations
Each surface changes only these, and its markup stays as it is:
- background: the tint.
- backdrop-filter: the material's filter, then the rim lens (step 3) last, so the color steps fold into one pass and the bend stays crisp.
- box-shadow: the edge.
- color: the ink, with the blend mode on everything drawn inside.
Hover brightens the tint by about 14% instead of lifting a shadow.

3. The rim lens
- Build a displacement map for each surface: red is the sideways bend, green the vertical one, 128 is none. Only a rim bends; the middle stays flat.
- The rim is about 44px wide on a card (min(44px, the short side / 3.5), never under 4px). For a thick slab, a point d from the edge looks inward by 1.1 × rim × (1 - d / rim)^2.6: hard at the edge, where the scene folds back on itself, easing to nothing at the rim's inner edge.
- Draw the map for the same corner the CSS draws. If the surface uses corner-shape: superellipse(), follow that curve, not a circle, or the bend and the rim light miss each other at the corners.
- Put it in an SVG filter with feImage (the map) and feDisplacementMap (scale = 2 × the largest offset), and add url(#id) at the end of backdrop-filter.
- Split it into colors: three passes, red 15% further and blue 15% less, each kept to its channel with feColorMatrix and added back with screen blends. Check the corners of wide rims over busy backgrounds; lower the split where it turns into a haze.
- Only Chromium draws url() in backdrop-filter. Check CSS.supports('backdrop-filter', 'url(#a)') and skip Firefox, which parses it and draws nothing. Everywhere else the CSS-only glass stays.
- Wait for the map image to decode before pointing a surface at the filter, or it draws without its blur. Give every filter a unique id and its own <svg>. Build maps once per radius and rim width and reuse them; on resize, move the map, don't rebuild it.
- Skip the lens on surfaces under 32px, and on surfaces with will-change or their own compositing layer (menus, animated panels), where Chrome can draw the map instead of the bent backdrop.

4. Adapt to what is behind
- For each surface, sample the background under it at a 5 × 3 grid, drop the two darkest and two brightest points, and average. Luminance is (0.299 R + 0.587 G + 0.114 B) / 255; texture is the brightest point minus the darkest.
- Ink: in light mode, light text when the luminance is under 0.5. Keep the current ink within ±0.05 of the line so a surface never flickers at it. Dark mode is always light text. Neighbouring surfaces in one row share one decision.
- Density (0 thin, 1 dense) = 0.55 × how close the background is to the ink's own brightness + 0.7 × texture + 0.3 × chroma. It moves the tint between its thin and dense alpha.
- Saturation kept = 1 - 0.75 × chroma, at least 0.45, so a vivid background doesn't stain the glass.
- Sample again when the background, the theme or a surface's position changes, and remember the last ink so the next load opens in it.
- If the background is not an image you can read (live video, cross-origin content), pick the ink from the theme and use the dense tint.

5. Let people set the tint
- Add one setting for light mode and one for dark that scale the tint, from 0 to 1.5 (1 is the material as designed). Below 1 the brightness lift in the filter thins with it, so at 0 only the blur, the rim and the shadow are left.
- Each theme keeps its own value; switching themes glides to that theme's value.

6. Keep it accessible
- Check text contrast on the busiest and the darkest background the product allows, at the default tint; raise density, not the ink's opacity, until it passes.
- Respect prefers-reduced-transparency and forced colors: fall back to a solid surface in the same tint at full opacity.
- Keep focus rings, disabled states and error colors as they are; failures keep their red.

7. Deliver
- First list the surfaces you will change, which get the lens and the color split, and which you leave solid and why.
- Then add the tokens, the lens helper and the two settings, switch the surfaces over, and list every file you touched.
- Don't change layout, fonts, radii or copy. If a surface would need a new component to work as glass, stop and ask me first.

质感来自可解释的细节

液态玻璃不是“透明 + 模糊”,而是一套关于光如何经过材料的规则。

只有边缘弯折,玻璃才有厚度;只有颜色从背景取样,透明才有意义;只有文字、图标和状态共享同一套 ink,内容才不会浮在材料之外。把这些关系放进 token,表面才能在任何背景和主题里保持同一个材质。

站内搜索

没有找到内容!